package cn.com.yusys.yusp.admin.web.rest;

import cn.com.yusys.yusp.admin.domain.AdminSmAuthReco;
import cn.com.yusys.yusp.admin.domain.AdminSmRole;
import cn.com.yusys.yusp.admin.domain.AdminSmUser;
import cn.com.yusys.yusp.admin.service.AdminSmRoleService;
import cn.com.yusys.yusp.commons.exception.MessageConstants;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;
import cn.com.yusys.yusp.commons.web.rest.CommonResource;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @项目名称：yusp-admin
 * @类名称：AcrmSmRoleResource @类描述：
 * @功能描述:
 * @创建人：zhanghan3@yusys.com.cn @创建时间：2017-12-14 15:45 @修改备注：
 * @修改日期 修改人员 修改原因 -------- -------- ----------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@RestController
@RequestMapping("api/adminsmrole")
public class AdminSmRoleResource extends CommonResource<AdminSmRole, String> {

	private Logger logger = LoggerFactory.getLogger(AdminSmRoleResource.class);

	@Autowired
	private AdminSmRoleService service;

	public AdminSmRoleResource(AdminSmRoleService service) {
		super();
		this.service = service;
	}

	@Override
	protected CommonService getCommonService() {
		return null;
	}
	
	

	/**
	 * 主页面分页查询
	 */
	@GetMapping("/querypage")
	public ResultDto<List<Map<String, Object>>> getList(QueryModel queryModel) {
		List<Map<String, Object>> list = service.queryPage(queryModel);
		return new ResultDto<List<Map<String, Object>>>(list);
	}

	/**
	 * 根据状态只查询生效角色
	 */
	@GetMapping("/querybyrolests")
	public ResultDto<List<Map<String, Object>>> getListByRolests(QueryModel queryModel) {
		List<Map<String, Object>> list = service.queryPage(queryModel);
		return new ResultDto<List<Map<String, Object>>>(list);
	}

	/**
	 * 新建角色保存
	 */
	@PostMapping("/createrole")
	public ResultDto<Object> createRole(@RequestBody AdminSmRole pool) {
		ResultDto<Object> map = new ResultDto<Object>();
		map = service.createRole(pool);
		return map;
	}

	/**
	 * 修改角色保存
	 */
	@PostMapping("/editrole")
	public ResultDto<Object> editRole(@RequestBody AdminSmRole pool) {
		String roleId = pool.getRoleId();
		ResultDto<Object> map = new ResultDto<Object>();
		service.editRole(pool);
		map.setData(roleId);
		return map;
	}

	/**
	 * 通过角色ID查询用户
	 */
	@GetMapping("/roleuser")
	public ResultDto<List<Map<String, Object>>> getRoleUser(QueryModel queryModel) {
		List<Map<String, Object>> list = service.getRoleUser(queryModel);
		return new ResultDto<List<Map<String, Object>>>(list);
	}

    /**
     *
     * @方法名称:deleteBatch
     * @方法描述:删除
     * @参数与返回说明:删除角色
     * @算法描述:
     */
    @PostMapping("/deletebatch")
    public ResultDto<Object> deleteBatch(@RequestBody Map<?,?> id) {
    	String ids=id.get("ids").toString();
        Map<String,Object> result=new HashMap<String, Object>();
        result= service.deleteRole(ids);
        return new ResultDto<Object>(result);
    }

	/**
	 * 
	 * @方法名称:useBatch
	 * @方法描述:启用
	 * @参数与返回说明:
	 * @算法描述:
	 */
	@SuppressWarnings("rawtypes")
    @PostMapping("/usebatch")
	public ResultDto<Object> useBatch(@RequestBody Map ids) {
		int n = 0;
		if (ids != null && !"".equals(ids.toString())) {
			String userId = ids.get("userId").toString();// 当前用户Id
			String date = DateUtil.formatDateTimeByDef();
			String[] idStr = ids.get("id").toString().split(",");
			for (int i = 0; i < idStr.length; i++) {
				if (!"".equals(idStr[i])) {
					n = n + this.service.useIngFn(idStr[i], userId, date);
				}
			}
			logger.info("Batch enable role [PK:{" + ids + "}] " + date);
		}
		ResultDto<Object> resultDto = new ResultDto<Object>();
		List<Object> i18nData = new ArrayList<Object>();
        resultDto.setCode(0);
        resultDto.setMessage("成功启用"+n+"条数据");
        i18nData.add(n);
        resultDto.setI18nData(i18nData);
		return resultDto;
	}

	/**
	 * 
	 * @方法名称: createcheckrolecode
	 * @方法描述: 新增检查角色编号是否重复
	 * @参数与返回说明:
	 * @算法描述:
	 */
	@GetMapping("/createcheckrolecode")
	public int createCheckrolecode(@RequestParam(required = false) String roleCode) {
		return this.service.createCheckrolecode(roleCode);
	}

	/**
	 * 
	 * @方法名称: editcheckrolecode
	 * @方法描述: 修改检查角色编号是否重复
	 * @参数与返回说明:
	 * @算法描述:
	 */
	@GetMapping("/editcheckrolecode")
	public int editCheckrolecode(@RequestParam(required = false) String roleCode, String roleId) {
		return this.service.editCheckrolecode(roleCode, roleId);
	}

	/**
	 * 
	 * @方法名称:unUseBatch
	 * @方法描述:停用
	 * @参数与返回说明:
	 * @算法描述:
	 */
	@SuppressWarnings("rawtypes")
    @PostMapping("/unusebatch")
	public ResultDto<Object> unUseBatch(@RequestBody Map ids) {
		int n = 0;
        Map<String,Object> result=new HashMap<String, Object>();
		if (ids != null && !"".equals(ids.toString())) {
            int count = 0;
			String userId = ids.get("userId").toString();// 当前用户Id
			String date = DateUtil.formatDateTimeByDef();
			String[] idStr = ids.get("id").toString().split(",");
			for (int i = 0; i < idStr.length; i++) {
				if (!"".equals(idStr[i])) {
				    int num = this.service.getUserByRoleId(idStr[i]);
				    if(num>0){
				        count++;
                    }else{
                        n = n + this.service.unUseIngFn(idStr[i], userId, date);
                    }
				}
			}
            if(count==0) {
                result.put("message", "成功停用"+n+"条数据!");
            }else {
                result.put("message", "成功停用"+n+"条数据,有"+count+"条数据由于存在用户数据，无法停用!");
            }
		}
		return new ResultDto<Object>(result);
	}
	
	
	
	//默认权限  begin add by chenlin
	/**
	 * 
	* @方法名称: getRoleAuthList
	* @方法描述: 角色默认权限
	* @参数与返回说明: 
	* @算法描述:
	 */
	@GetMapping("/roleauth")
	public ResultDto<List<AdminSmAuthReco>> getRoleAuthList(QueryModel queryModel) {
		List<AdminSmAuthReco> list = service.getRoleAuthList(queryModel);
		return new ResultDto<List<AdminSmAuthReco>>(list);
	} 
	/**
	 * 
	* @方法名称: saveRoleAuth
	* @方法描述: 新增默认权限
	* @参数与返回说明: 
	* @算法描述:
	 */
	@PostMapping("/saveroleauth")
	public int saveRoleAuth(@RequestBody AdminSmAuthReco pool) {
		return this.service.saveRoleAuth(pool);
	}
	/**
	 * 
	* @方法名称: updateRoleAuth
	* @方法描述: 修改默认权
	* @参数与返回说明: 
	* @算法描述:
	 */
	@PostMapping("/updateroleauth")
	public int updateRoleAuth(@RequestBody Map<?, ?> parmas) {
		String authId = parmas.get("authId").toString();
        String userId = parmas.get("userId").toString();
        String authTmplId = parmas.get("authTmplId").toString();
        Map<String, String> paramMap=new HashMap<String, String>();
    	paramMap.put("authId", authId);
    	paramMap.put("authTmplId", authTmplId);
		paramMap.put("lastChgUsr", userId);
		return this.service.updateRoleAuth(paramMap);
	}
	/**
	 * 
	* @方法名称: cleanRoleAuth
	* @方法描述: 清除角色默认权限
	* @参数与返回说明: 
	* @算法描述:
	 */
	@GetMapping("/cleanroleauth")
	public int cleanRoleAuth(@RequestParam(required = false) String authId){
		return this.service.cleanRoleAuth(authId);
	}
	//默认权限 end
	
    /**
    *
    * @方法名称:addRoleUser
    * @方法描述:添加角色用户
    * @参数与返回说明:新增角色用户
    * @算法描述:
    */
   @PostMapping("/addroleuser")
   public ResultDto<Object> addRoleUser(@RequestBody Map<String, Object> param) {
	   ResultDto<Object> result=this.service.addRoleUser(param);
	   return result;
   }
   /**
   *
   * @方法名称:delRoleUser
   * @方法描述:删除角色用户
   * @参数与返回说明:删除角色用户
   * @算法描述:
   */
   @PostMapping("/delroleuser")
   public ResultDto<Integer> delRoleUser(@RequestBody Map<String, Object> param) {
	   return new ResultDto<Integer>(this.service.delRoleUser(param)); 
   }
}
